package com.sqisland.dash;

import android.os.Bundle;
import android.content.Intent;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class DashboardActivity extends BaseActivity {
  private Button airspeedSensorName;
  private TextView airspeedSensorDisplay;
  private Button textSensorName;
  private TextView textSensorDisplay;
  private TextView connectButton;

  private AirspeedSensorManager.Listener airspeedListener
      = new AirspeedSensorManager.Listener() {
    private char[] hexArray = "0123456789abcdef".toCharArray();

    @Override
    public void onMessageReceived(byte[] buffer, int length) {
      StringBuffer stringBuffer = new StringBuffer();

      int start = Math.min(8, length);
      for (int i = start; i >= 1; --i) {
        int v = buffer[length - i] & 0xFF;
        stringBuffer.append(hexArray[v >>> 4]);
        stringBuffer.append(hexArray[v & 0x0F]);
        stringBuffer.append(" ");
      }

      stringBuffer.append("\nLength = ");
      stringBuffer.append(String.valueOf(length));

      airspeedSensorDisplay.setText(stringBuffer.toString());
    }
  };

  private TextSensorManager.Listener textListener
      = new TextSensorManager.Listener() {
    @Override
    public void onMessageReceived(String msg) {
      textSensorDisplay.setText(msg);
    }
  };

  private View.OnClickListener sensorNameOnClickListener
      = new View.OnClickListener() {
    @Override
    public void onClick(View v) {
      Intent intent = new Intent(
          DashboardActivity.this, BluetoothDevicesActivity.class);
      switch (v.getId()) {
      case R.id.airspeed_sensor_name:
        intent.putExtra(Constants.KEY_TYPE, AirspeedSensorManager.TYPE);
        break;
      case R.id.text_sensor_name:
        intent.putExtra(Constants.KEY_TYPE, TextSensorManager.TYPE);
        break;
      }
      startActivity(intent);
    }
  };

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_dashboard);

    airspeedSensorName = (Button) findViewById(R.id.airspeed_sensor_name);
    airspeedSensorDisplay
        = (TextView) findViewById(R.id.airspeed_sensor_display);
    textSensorName = (Button) findViewById(R.id.text_sensor_name);
    textSensorDisplay = (TextView) findViewById(R.id.text_sensor_display);
    connectButton = (TextView) findViewById(R.id.connect_button);

    airspeedSensorName.setOnClickListener(sensorNameOnClickListener);
    textSensorName.setOnClickListener(sensorNameOnClickListener);
    connectButton.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View v) {
        if (bound) {
          sensorsService.toggleDeviceConnections();
          updateViews();
        }
      }
    });
  }

  @Override
  protected void onSensorServiceConnected() {
    updateViews();
    addListeners();
  }

  @Override
  protected void onResume() {
    super.onResume();
    updateViews();
    addListeners();
  }

  @Override
  protected void onPause() {
    super.onPause();
    removeListeners();
  }

  private void addListeners() {
    if (!bound) {
      return;
    }
    AirspeedSensorManager airspeedSensorManager
        = (AirspeedSensorManager) sensorsService.getSensor(
            AirspeedSensorManager.TYPE);
    if (airspeedSensorManager != null) {
      airspeedSensorManager.addListener(airspeedListener);
    }
    TextSensorManager textSensorManager
        = (TextSensorManager) sensorsService.getSensor(TextSensorManager.TYPE);
    if (textSensorManager != null) {
      textSensorManager.addListener(textListener);
    }
  }

  private void removeListeners() {
    if (!bound) {
      return;
    }
    AirspeedSensorManager airspeedSensorManager
        = (AirspeedSensorManager) sensorsService.getSensor(
            AirspeedSensorManager.TYPE);
    if (airspeedSensorManager != null) {
      airspeedSensorManager.removeListener(airspeedListener);
    }
    TextSensorManager textSensorManager
        = (TextSensorManager) sensorsService.getSensor(TextSensorManager.TYPE);
    if (textSensorManager != null) {
      textSensorManager.removeListener(textListener);
    }
  }

  private void clearDisplay() {
    airspeedSensorDisplay.setText("\n");
    textSensorDisplay.setText(null);
  }

  private void updateViews() {
    if (bound) {
      AirspeedSensorManager airspeedSensorManager
          = (AirspeedSensorManager) sensorsService.getSensor(
              AirspeedSensorManager.TYPE);
      if (airspeedSensorManager != null) {
        airspeedSensorName.setText(airspeedSensorManager.getName());
      } else {
        airspeedSensorName.setText(R.string.none);
      }

      TextSensorManager textSensorManager
          = (TextSensorManager) sensorsService.getSensor(
              TextSensorManager.TYPE);
      if (textSensorManager != null) {
        textSensorName.setText(textSensorManager.getName());
      } else {
        textSensorName.setText(R.string.none);
      }
    }

    if (bound && sensorsService.isConfigured()) {
      connectButton.setEnabled(true);
    } else {
      connectButton.setEnabled(false);
    }

    if (bound && !sensorsService.isDisconnected()) {
      connectButton.setText(R.string.disconnect_all);
    } else {
      clearDisplay();
      connectButton.setText(R.string.connect_all);
    }
  }
}
